﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RW.Data;
using RW.Data.Graph;
using RW.Data.Objects;

namespace RW.QueriesWindows
{
    /// <summary>
    /// Interaction logic for Query2Window.xaml
    /// </summary>
    public partial class Query2Window : Window
    {
        private Query2 query2;

        public enum truefalse
        {
            TRUE = 1,
            FALSE = 0
        }

        public Query2Window(Problem problem, MultiGraph graph)
        {
            InitializeComponent();
            PossibleActionsCombo.ItemsSource = problem.Actions;
            PossibleVariablesCombo.ItemsSource = problem.Variables;
            TrueFalseCombo.Items.Add(truefalse.TRUE);
            TrueFalseCombo.Items.Add(truefalse.FALSE);
            ProgramTextBlock.TextWrapping = TextWrapping.Wrap;
            query2 = new Query2(graph);
        }



        //PROGRAM
        private void AddActionButton_Click(object sender, RoutedEventArgs e)
        {
            if (PossibleActionsCombo.SelectedItem == null)
                return;
            ProgramTextBlock.Text += String.Concat(" -> ", PossibleActionsCombo.SelectedItem.ToString());
            query2.Program.Add(PossibleActionsCombo.SelectedItem.ToString());
        }

        private void ClearProgramButton_Click(object sender, RoutedEventArgs e)
        {
            query2.Program.Clear();
            ProgramTextBlock.Text = string.Empty;
        }
        //***


        //CONDITIONS
        private void AddVariableButton_Click(object sender, RoutedEventArgs e)
        {
            if (PossibleVariablesCombo.SelectedItem == null || TrueFalseCombo.SelectedItem == null)
                return;
            bool key = ((int)TrueFalseCombo.SelectedValue > 0) ? true : false;
            try
            {
                query2.Conditions.Add(PossibleVariablesCombo.SelectedItem.ToString(), key);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("You cannot add the same variable twice!");
                return;
            }
            ConditionTextBox.Text += String.Concat(PossibleVariablesCombo.SelectedItem.ToString(), " : ", key, ", ");
        }

        private void ClearStartingCondition_Click(object sender, RoutedEventArgs e)
        {
            query2.Conditions.Clear();
            ConditionTextBox.Clear();
        }
        //***
        

        //TARGET
        private void AddTargetVariableButton_Click(object sender, RoutedEventArgs e)
        {
            if (PossibleVariablesCombo.SelectedItem == null || TrueFalseCombo.SelectedItem == null)
                return;
            bool key = ((int)TrueFalseCombo.SelectedValue > 0) ? true : false;
            try
            {
                query2.Target.Add(PossibleVariablesCombo.SelectedItem.ToString(), key);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("You cannot add the same variable twice!");
                return;
            }
            TargetTextBox.Text += String.Concat(PossibleVariablesCombo.SelectedItem.ToString(), " : ", key, ", ");
        }

        private void ClearTarget_Click(object sender, RoutedEventArgs e)
        {
            query2.Target.Clear();
            TargetTextBox.Clear();
        }
        //***
        

        //EXECUTE
        private void ExecuteQueryAlways_Click(object sender, RoutedEventArgs e)
        {
            string message = CheckInput();
            if (message != null)
            {
                MessageBox.Show(message);
                return;
            }
            MessageBox.Show(String.Concat("The result of the query is: ", query2.Compute(true)));
        }

        private void ExecuteQueryExists_Click(object sender, RoutedEventArgs e)
        {
            string message = CheckInput();
            if (message != null)
            {
                MessageBox.Show(message);
                return;
            }
            MessageBox.Show(String.Concat("The result of the query is: ", query2.Compute(false)));
        }
        //***



        //OTHER METHODS
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private string CheckInput()
        {
            if (query2.Program.Count == 0)
                return "Program has to include at least 1 command!";
            if (query2.Target.Count != PossibleVariablesCombo.Items.Count)
                return "Target has to include all possible variables!";
            return null;
        }
        //***


    }
}
